package edu.zut.main.thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 使用多线程模拟同时下载多个图片
 */
public class ThreadDemo2 {

    public static void main(String[] args) {
        ImgDownThread imgDownThread1 = new ImgDownThread("https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E7%A8%8B%E5%BA%8F%E5%91%98&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=&copyright=&cs=4062270150,1645648084&os=1378497615,2153932464&simid=3372916285,4241222753&pn=10&rn=1&di=21450&ln=1273&fr=&fmq=1630670361205_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fbpic.588ku.com%252Felement_origin_min_pic%252F18%252F06%252F09%252F3fcdd76c9638e4f66ec2e0f50816db33.jpg%26refer%3Dhttp%253A%252F%252Fbpic.588ku.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1633262361%26t%3D8fdaafcbb97cd5764cdf4d8736dee924&rpstart=0&rpnum=0&adpicid=0&nojc=undefined", "1.jpg");
        ImgDownThread imgDownThread2 = new ImgDownThread("https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E7%A8%8B%E5%BA%8F%E5%91%98&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=&copyright=&cs=1792531100,1626220125&os=2810627918,2670302280&simid=3383035388,232878641&pn=8&rn=1&di=150040&ln=1273&fr=&fmq=1630670361205_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252F5b0988e595225.cdn.sohucs.com%252Fimages%252F20171127%252Fe2fd70ca06d44c7d8c48c37f7e66689a.jpeg%26refer%3Dhttp%253A%252F%252F5b0988e595225.cdn.sohucs.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1633262361%26t%3D6185c2528a6b085e5c14d4929f25ee0e&rpstart=0&rpnum=0&adpicid=0&nojc=undefined", "2.jpg");
        //启动创建好的下载图片线程(start 是让当前线程处于就绪，具体执行哪一个cpu决定)
        imgDownThread1.start();
        imgDownThread2.start();
    }
}

/**
 * 新建一个线程类，用于下载图片
 * 1、新建一个类继承Thread，重写run方法
 * 2、实例化当前线程类，调用start即可
 */
class ImgDownThread extends Thread{

    private String url;
    private String fileName;

    public ImgDownThread(String url,String fileName){
        this.url = url;
        this.fileName = fileName;
    }

    //下载图片线程任务,线程任务是自动执行的
    @Override
    public void run() {
        new ImgDown().down(url,fileName);
        System.out.println("下载的是："+fileName);
    }
}

//封装一个下载类
class ImgDown{

    /**
     * 下载图片
     * 使用commons-io工具类进行图片的下载
     * FileUtils:文件操作的工具类
     * copyURLToFile： 从url地址上拷贝一个文件到目标文件
     */
    public void down(String url,String fileName){
        //该方法的第一个参数是URL地址，第二个参数是目标文件的位置
        try {
            FileUtils.copyURLToFile(new URL(url),new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


